package com.fieldchain.ex.fa100.ui.personal.activity;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;

import com.fieldchain.ex.fa100.utils.common.VerticalUtils;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.common.EmailCode;
import com.fieldchain.ex.fa100.utils.net.modle.ImageRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/15
 */
public class ChangeEmailNext extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private EditText etPhone;
    private EditText etImgCode;
    private ImageView ivImgcode;
    private EditText etSmsCode;
    private TextView tvSms;
    private TextView tvSure;

    @Override
    public void findView() {
        getImage();
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        etPhone = findViewById(R.id.et_phone);
        etImgCode = findViewById(R.id.et_imgCode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etSmsCode = findViewById(R.id.et_smsCode);
        tvSms = findViewById(R.id.tv_sms);
        tvSure = findViewById(R.id.tv_sure);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VerticalUtils.getInstance().cancle();
    }

    @Override
    public void setValue() {
        tvCenter.setText("修改邮箱");
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        ivImgcode.setOnClickListener(this);
        tvSms.setOnClickListener(this);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_emailnext;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_imgcode:
                getImage();
                break;
            case R.id.tv_sms:
                VerticalUtils.getInstance().start(tvSms);
                EmailCode.getInstance().getEmail(etPhone.getText().toString(), etImgCode.getText().toString());
                break;
            case R.id.tv_sure:
                send();
                break;
            case R.id.rea_back:
                finish();
                break;
        }
    }

    public void getImage() {
        HttpRequest.getInstance().getImage(Constant.image, new ImageRequest() {
            @Override
            public void imgReceive(Bitmap bit) {
                ivImgcode.setImageBitmap(bit);
            }
        });
    }

    public void send() {
        String sms = etSmsCode.getText().toString();
        String email = etPhone.getText().toString();
        if (email.length() == 0) {
            MyToast.show("请输入新邮箱");
            return;
        }
        if (sms.length() == 0) {
            MyToast.show("请输入邮箱验证码");
            return;
        }
        HashMap map = new HashMap();
        map.put("email", email);
        map.put("newCode", sms);
        map.put("step", 2+"");
        HttpRequest.getInstance().postRequest(map, Constant.changeEmail, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                if (code == 200) {
                    finish();
                }
                MyToast.show(msg);
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
